package org.foj.model {

import flash.utils.setTimeout;

import mx.core.mx_internal;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;

use namespace mx_internal;

public class MockServiceHelper {
  public function MockServiceHelper() {
  }

  public static function createAsyncToken(serviceResult:Object = null):AsyncToken
  {
    var token:AsyncToken = new AsyncToken(null);
    setTimeout(applyResultOnToken, Math.random() * 3000, token, serviceResult);
    return token;
  }

  private static function applyResultOnToken(token:AsyncToken, serviceResult:Object):void
  {
    token.setResult(serviceResult);
    var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, serviceResult, token);
    token.applyResult(event);
  }
}
}